'use strict';
const dayjs = require("dayjs");
module.exports = app => {
  const { INTEGER, STRING, TEXT, DECIMAL } = app.Sequelize;
  const Staff = app.model.define(
      'staff',
      {
          id: {type: INTEGER, primaryKey: true, autoIncrement: true},
            nickname: STRING(255),
            phone: STRING(255),
            gender: STRING(11),
            password: STRING(255),
            age: INTEGER(11),
            city_id: INTEGER(11),
            status: INTEGER(11),
            audit_status: INTEGER(11),
            avatar: STRING(255),
            photos: TEXT,
            description: STRING(255),
            content: TEXT,
            emergency_contact_name: STRING(255),
            emergency_contact_phone: STRING(255),
            is_recommend: INTEGER(11),
            sort: INTEGER(11),
            order_time: STRING(255),
            order_status: INTEGER(11),
            is_del: INTEGER(11),
            card_no: STRING(255),
            card_front: STRING(255),
            card_back: STRING(255),
            card_hand: STRING(255),
            health_certificate: STRING(255),
            other_certificate: STRING(255),
            skill_introduction: STRING(255),
            member_id: INTEGER(11),
            latitude: STRING(255),
            longitude: STRING(255),
            is_online: INTEGER(11),
            balance: DECIMAL(10, 2),
            sale_count: INTEGER,
           // 结算比例
            settlement_ratio: INTEGER,
          pdf_url: STRING(255),
          service_type: STRING(255),
          address_name: STRING(255),
          address: STRING(255),
            create_time: {
                type: app.Sequelize.DATE,
                allowNull: true,
                get() {
                    return dayjs(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
                }
            },
            updated_time: {
                type: app.Sequelize.DATE,
                allowNull: true,
                get() {
                    return dayjs(this.getDataValue('updated_time')).format('YYYY-MM-DD HH:mm:ss');
                }
            }
      },
      {
          tableName: 'staff',
          timestamps: true, // 启用时间戳(createdAt, updatedAt)
          updatedAt: 'updated_time', // 不想要 updatedAt
          createdAt: 'create_time', // 想要  createdAt 但是希望名称叫做 created_at
      }
  );
    Staff.associate = function() {
        // 城市
        app.model.Staff.belongsTo(app.model.City, { foreignKey: 'city_id', targetKey: 'id' });
        // 接了多少订单
        app.model.Staff.hasMany(app.model.OrderGoods, { foreignKey: 'staff_id', targetKey: 'id' });
        // 有多少收藏
        app.model.Staff.hasMany(app.model.StaffCollect, { foreignKey: 'staff_id', targetKey: 'id' });
        // 有多少评论
        app.model.Staff.hasMany(app.model.StaffComment, { foreignKey: 'staff_id', targetKey: 'id' });
    }
  return Staff
};
